package com.wzx.ptplatform.service.impl;

import com.wzx.ptplatform.dao.CompanyDao;
import com.wzx.ptplatform.domain.Company;
import com.wzx.ptplatform.domain.Student;
import com.wzx.ptplatform.service.CompanyService;
import com.wzx.ptplatform.utils.PtplatformException;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Predicate;
import java.util.List;

/**
 * @author 紫煦桉
 */
@Service
public class CompanyServiceImpl implements CompanyService {

    @Resource
    private CompanyDao companyDao;

    @Override
    public List<Company> findAll() throws PtplatformException {
        return companyDao.findAll();
    }

    @Override
    public Page<Company> findList(Company company, Integer page, Integer limit) throws PtplatformException {
        Specification<Company> spec = (Specification<Company>) (root, criteriaQuery, criteriaBuilder) -> {
            Predicate predicate = criteriaBuilder.conjunction();
            List<Expression<Boolean>> exList = predicate.getExpressions();//动态SQL表达式集合
            if (company.getCompanyName() != null) {
                exList.add(criteriaBuilder.like(root.get("companyName").as(String.class), "%" + company.getCompanyName() + "%"));
            }
            if (company.getIndustry() != null && company.getIndustry().getId() != null) {
                exList.add(criteriaBuilder.equal(root.get("industry").get("id").as(Long.class), company.getIndustry().getId()));
            }
            if (company.getCity() != null && company.getCity().getId() != null) {
                exList.add(criteriaBuilder.equal(root.get("city").get("id").as(Long.class), company.getCity().getId()));
            }
            return predicate;
        };
        PageRequest pageable = PageRequest.of(page, limit, Sort.Direction.DESC, "id");
        return companyDao.findAll(spec, pageable);
    }

    @Override
    public boolean saveOrEdit(Company company) throws PtplatformException {
        try {
            companyDao.saveAndFlush(company);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public Company findById(long id) throws PtplatformException {
        return companyDao.findById(id).orElse(null);
    }

    @Override
    public boolean deleteById(long id) throws PtplatformException {
        try {
            companyDao.deleteById(id);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public Company login(Company company) throws PtplatformException {
        Company companyMessage = companyDao.findByPhone(company.getPhone());
        if (companyMessage != null && companyMessage.getPassword().equals(company.getPassword())) {
            return companyMessage;
        }
        return null;
    }
}
